package com.cobla.struct.queue;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 链队列
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class LinkedQueue {
    private QNode front;
    private QNode rear;

    /**
     * 队列是否为空
     * @return
     */
    public boolean isEmpty() {
        return (this.front == null || this.rear == null);
    }

    /**
     * 入队
     * @param x
     */
    public void enQueue(int x) {
        QNode q = new QNode(x, null);
        // queue empty
        if (this.rear == null) {
            this.front = this.rear = q;
        } else {
            this.rear.setNext(q);
            this.rear = this.rear.getNext();
        }
    }

    /**
     * 出队
     * @return 队空返回 null
     */
    public Integer deQueue() {
        if (this.front == null) {
            return null;
        }
        if (this.front == this.rear) {
            this.rear = null;
        }
        int data = this.front.getData();
        this.front = this.front.getNext();
        return data;
    }

}

/**
 * 链队列 节点
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
class QNode{
    private int data;
    private QNode next;
}
